package com.lagou.web;

import com.lagou.dao.StudentDao;
import com.lagou.domain.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Administrator
 */
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Student student = new Student();

        String name = req.getParameter("name");
        String sex = req.getParameter("sex");

        // String 类型按照 yyyy-MM-dd 的格式转换为 java.util.Date 类
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date birthday = simpleDateFormat.parse(req.getParameter("birthday"));
            student.setStname(name);
            student.setSex(sex);
            student.setBirthday(birthday);
            new StudentDao().add(student);

            req.getRequestDispatcher("/listStu").forward(req,resp);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
